package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.ISignUpRoomDao;
import com.eling.elcms.travel.model.CheckInCar;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.NonMemGroupReserve;
import com.eling.elcms.travel.model.SignUpRoom;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import com.eling.elcms.travel.service.INonMemGroupReserverManager;
import com.eling.elcms.travel.service.ISignUpRoomManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SignUpRoomManagerImpl extends GenericManagerImpl<SignUpRoom, Long> implements ISignUpRoomManager {
	ISignUpRoomDao signUpRoomDao;

	@Autowired
	private INonMemGroupReserverManager nmgReserverManager;

	@Autowired
	private ICheckInImplementManager ciiManager;

	@Autowired
	public void setDao(ISignUpRoomDao dao) {
		this.dao = dao;
		this.signUpRoomDao = dao;
	}

	@Override
	public List<SignUpRoom> queryByReserve(Long pkNonMemGroupReserve) {
		NonMemGroupReserve nmgReserve = nmgReserverManager.get(pkNonMemGroupReserve);
		List<SignUpRoom> returnList = new ArrayList<SignUpRoom>(nmgReserve.getSignUpRooms());
		return returnList;
	}

	@Override
	public SignUpRoom updateStatus(Long pkNonMemGroupReserve, Long pkSignUpRoom) {
		if (null != pkSignUpRoom) { // 更新子表状态
			SignUpRoom signUpRoom = signUpRoomDao.get(pkSignUpRoom);
			signUpRoom.setRoomStatus(CheckStatus.Pended);
			signUpRoom = save(signUpRoom);
			NonMemGroupReserve nmgReserve = nmgReserverManager.get(pkNonMemGroupReserve);
			List<SignUpRoom> signUpRoomList = new ArrayList<SignUpRoom>(nmgReserve.getSignUpRooms());
			boolean flag = true;
			for (SignUpRoom sur : signUpRoomList) {
				if (sur.getRoomStatus().equals(CheckStatus.Pending)) {
					flag = false;
				}
			}
			if (flag) { // 如果子表都已落实，则更新落实单主表并落实
				CheckInImplement cii = new CheckInImplement();
				cii.setNonMemGroupReserve(nmgReserve);
				List<CheckInImplement> ciiList = ciiManager.query(cii);
				if (!ciiList.isEmpty()) {
					cii = ciiList.get(0);
					cii.setRoomStatus(CheckStatus.Pended);
					// 落实单餐饮和车辆配置落实了，主表状态改为落实
					if (cii.getCheckInDinner() !=null && (cii.getCheckInDinner().getStatus().equals(CheckStatus.Pended)
							|| cii.getCheckInDinner().getStatus().equals(CheckStatus.NoRequiement))) {
						Boolean fg = false;
						Set<CheckInCar> checkInCar = cii.getCheckInCar();
						if (checkInCar != null && !checkInCar.isEmpty()) {
							for (CheckInCar CheckinCar : checkInCar) {
								if (CheckinCar.getStatus().equals(CheckStatus.Pended)
										|| CheckinCar.getStatus().equals(CheckStatus.NoRequiement)) {
									fg = true;
								}
							}
						}
						if (fg) {
							cii.setStatus(CheckStatus.Confirmed);
						}
					}
					ciiManager.save(cii);
					// 修改主表总状态
					ciiManager.setCiiCheckStatusConfirm(cii);
				}
			}
		} else { // 更新主表状态,且落实所有子表
			NonMemGroupReserve nmgReserve = nmgReserverManager.get(pkNonMemGroupReserve);
			List<SignUpRoom> signUpRoomList = new ArrayList<SignUpRoom>(nmgReserve.getSignUpRooms());
			for (SignUpRoom sur : signUpRoomList) {
				sur.setRoomStatus(CheckStatus.Pended);
			}
			signUpRoomDao.save(signUpRoomList);
			CheckInImplement cii = new CheckInImplement();
			cii.setNonMemGroupReserve(nmgReserve);
			List<CheckInImplement> ciiList = ciiManager.query(cii);
			if (!ciiList.isEmpty()) {
				cii = ciiList.get(0);
				cii.setRoomStatus(CheckStatus.Pended);
				// 落实单餐饮和车辆配置落实了，主表状态改为落实
				if ((cii.getCheckInDinner().getStatus().equals(CheckStatus.Pended)
						|| cii.getCheckInDinner().getStatus().equals(CheckStatus.NoRequiement))) {
					Boolean fg = false;
					Set<CheckInCar> checkInCar = cii.getCheckInCar();
					if (checkInCar != null && !checkInCar.isEmpty()) {
						for (CheckInCar CheckinCar : checkInCar) {
							if (CheckinCar.getStatus().equals(CheckStatus.Pended)
									|| CheckinCar.getStatus().equals(CheckStatus.NoRequiement)) {
								fg = true;
							}
						}
					}
					if (fg) {
						cii.setStatus(CheckStatus.Confirmed);
					}
				}
				ciiManager.save(cii);
				// 修改主表总状态
				ciiManager.setCiiCheckStatusConfirm(cii);
			}
		}
		return new SignUpRoom();
	}
}
